package com.partsoft.dits;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.util.Assert;

@SuppressWarnings("unchecked")
public class BaseRegistryDescriptorResolver extends AbstractDescriptorResolver {
	
	protected Map<Class<? extends EdgeDescriptor>, DescriptorRegistry<? extends EdgeDescriptor>> classRegistryMap = Collections.synchronizedMap(new HashMap<Class<? extends EdgeDescriptor>, DescriptorRegistry<? extends EdgeDescriptor>>(2));
	
	public void addRegistry(DescriptorRegistry<? extends EdgeDescriptor> registry) {
		Assert.notNull(registry);
		Assert.isTrue(!classRegistryMap.containsKey(registry.getSupportClass()));
		classRegistryMap.put(registry.getSupportClass(), registry);
	}
	
	public void removeRegistry(DescriptorRegistry<? extends EdgeDescriptor> registry) {
		Assert.notNull(registry);
		classRegistryMap.remove(registry.getSupportClass());
	}
	
	public void removeRegistry(Class<? extends EdgeDescriptor> clazz) {
		Assert.notNull(clazz);
		classRegistryMap.remove(clazz);
	}
	
	public void clearRegistries() {
		classRegistryMap.clear();
	}
	

	public boolean hasDescriptor(Class<? extends EdgeDescriptor> dcls, String id) {
		boolean result = false;
		if (classRegistryMap.containsKey(dcls)) {
			result = classRegistryMap.get(dcls).hasDescriptor(id);
		}
		return result;
	}

	public <E extends EdgeDescriptor> E getDescriptor(Class<? extends EdgeDescriptor> dcls, String id) {
		EdgeDescriptor result = null;
		if (classRegistryMap.containsKey(dcls)) {
			result =classRegistryMap.get(dcls).getDescriptor(id);
		}
		return (E)result;
	}

}
